<#macro SqlBaseColumnList table id prex>
    <sql id="${id}">
        <#list table.columnList as column><#if column_index gt 0><#if column_index%5==0>${" \n"}        </#if>,</#if>${prex?default("")}${column.sqlName}</#list>
    </sql>
</#macro>

<#macro BaseColumnList table>
    <@SqlBaseColumnList table,"Base_Column_List"," " />
</#macro>

<#macro Base_SF_Column_List table>
    <@SqlBaseColumnList table,"Base_SF_Column_List"," sf." />
</#macro>

<#macro insertMethod table>
            <#if dalgen.dbType=="MySQL">
            <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
                SELECT
                LAST_INSERT_ID()
            </selectKey>
            </#if>
            <![CDATA[
            INSERT INTO ${table.sqlName}(
                <include refid="Base_Column_List"/>
            )VALUES(
                <#list table.columnList?sort_by("ordinalPosition") as column>
                    <#if column_index gt 0>,</#if>  <#if dalgen.dbType?upper_case=="ORACLE" && column.sqlName?upper_case == "ID">SEQ_${table.sqlName?upper_case}.nextval <#else>${lib.insertVal(column,dalgen)}</#if>
                </#list>
            )
            ]]>
</#macro>

<#macro mysqlUpdateBatchMethod table>
    <#if dalgen.dbType?lower_case=="mysql">
        <#if table.primaryKeys??>
    <operation name="updateBatch" paramtype="objectList" remark="批量更新表:${table.sqlName}">
        update
        (
        <foreach collection="list" index="index" item="item" open=""
                 close="" separator="union all">
            SELECT <#list table.columnList as column><#if column_index gt 0>,</#if>  ${lib.insertItemVal(column,dalgen)} ${column.sqlName}</#list>
            FROM dual
        </foreach>
        ) t
        join ${table.sqlName} u
        on (<#list table.primaryKeys.columnList as column> <#if column_index gt 0>AND </#if>u.${column.sqlName} = t.${column.sqlName} </#list>)
        set
        <#list table.columnList as column>
            <#if column_index gt 0>,</#if>u.${column.sqlName} = t.${column.sqlName}
        </#list>
    </operation>
        </#if>
    <#list table.uniqueIndexs as uniqueIndex>
        <operation name="updateBatchBy${uniqueIndex.ukName}" paramtype="objectList" remark="批量更新表:${table.sqlName}">
            update
            (
            <foreach collection="list" index="index" item="item" open="" close="" separator="union all">
                SELECT <#list table.columnList as column><#if column_index gt 0>,</#if>  item.${lib.insertItemVal(column,dalgen)} ${column.sqlName}</#list>
                FROM dual
            </foreach>
            ) t
            join ${table.sqlName} u
            on (
            <#list uniqueIndex.columnList as column>
                <#if column_index gt 0>AND </#if>u.${column.sqlName}${lib.space(column.sqlName)} = t.${column.sqlName}
            </#list>
            )
            set
            <#list table.columnList as column>
                <#if column_index gt 0>,</#if>u.${column.sqlName} = t.${column.sqlName}
            </#list>
        </operation>
    </#list>
    </#if>
</#macro>

<#macro oracleUpdateBatchMethod table>
    <#if dalgen.dbType?lower_case=="oracle" && table.primaryKeys??>
    <operation name="updateBatch" paramtype="objectList" remark="批量更新表:${table.sqlName}">
        merge into ${table.sqlName} u
        using
        (
        <foreach collection="list" index="index" item="item" open=""
                 close="" separator="union">
            SELECT <#list table.columnList as column><#if column_index gt 0>,</#if>  <#if dalgen.dbType?upper_case=="ORACLE" && column.sqlName?upper_case == "ID">SEQ_${table.sqlName?upper_case}.nextval <#else>${lib.insertVal(column,dalgen)}</#if> ${column.sqlName}</#list>
            FROM dual
        </foreach>
        ) t
        on (<#list table.primaryKeys.columnList as column> <#if column_index gt 0>AND </#if>u.${column.sqlName} = t.${column.sqlName} </#list>)
        when matched then
        update set
        <#list table.columnList as column>
            <#if column_index gt 0>,</#if>u.${column.sqlName} = t.${column.sqlName}
        </#list>
        when not matched then
        insert (
        <#list table.columnList as column><#if column_index gt 0>,</#if>${column.sqlName}</#list>)
        VALUES(
        <#list table.columnList as column><#if column_index gt 0>,</#if>t.${column.sqlName}</#list>
        )
    </operation>
    </#if>
</#macro>
<#macro oracleUpdateBatchMethodUK table>
    <#if dalgen.dbType?lower_case=="oracle" && table.uniqueIndexs?? && table.uniqueIndexs?size gt 0>
     <#assign uniqueIndex = table.uniqueIndexs?first/>
    <operation name="updateBatchUK" paramtype="objectList" remark="批量更新表:${table.sqlName}">
        merge into ${table.sqlName} u
        using
        (
        <foreach collection="list" index="index" item="item" open=""
                 close="" separator="union">
            SELECT <#list table.columnList as column><#if column_index gt 0>,</#if>  <#if dalgen.dbType?upper_case=="ORACLE" && column.sqlName?upper_case == "ID">SEQ_${table.sqlName?upper_case}.nextval <#else>${lib.insertVal(column,dalgen)}</#if> ${column.sqlName}</#list>
            FROM dual
        </foreach>
        ) t
        on (<#list uniqueIndex.columnList as column> <#if column_index gt 0>AND </#if>u.${column.sqlName} = t.${column.sqlName} </#list>)
        when matched then
        update set
        <#list table.columnList as column>
            <#if column_index gt 0>,</#if>u.${column.sqlName} = t.${column.sqlName}
        </#list>
        when not matched then
        insert (
        <#list table.columnList as column><#if column_index gt 0>,</#if>${column.sqlName}</#list>)
        VALUES(
        <#list table.columnList as column><#if column_index gt 0>,</#if>t.${column.sqlName}</#list>
        )
    </operation>
    </#if>
</#macro>

<#macro insertBatchMethod table>
    <#if dalgen.dbType?lower_case=="oracle">
        INSERT INTO ${table.sqlName}(
            <include refid="Base_Column_List"/>
        )
        <foreach collection="list"  item="item" separator="union all">
        select
            <#list table.columnList?sort_by("ordinalPosition") as column>
            <#if column_index gt 0>,</#if> ${lib.insertBatchVal(column,dalgen)}
            </#list>
        from dual
        </foreach>
    </#if>
    <#if dalgen.dbType?lower_case=="mysql">
            INSERT INTO ${table.sqlName}(
                <#list table.columnList as column>
                    <#if column_index gt 0>,</#if>${column.sqlName}
                </#list>
            )VALUES
            <foreach collection="list"  item="item" separator=",">
                (
                <#list table.columnList as column>
                    <#if column_index gt 0>,</#if> ${lib.insertBatchVal(column,dalgen)}
                </#list>
                )
            </foreach>
    </#if>
</#macro>

<#macro updateMethod table>
        <![CDATA[
        UPDATE ${table.sqlName}
        SET
            <#assign c_idx = 0>
            <#list table.columnList as column>
                <#if lib.updateIncludeColumn(dalgen,column,table.primaryKeys.columnList)><#assign c_idx = c_idx+1>
                    <#if c_idx gt 1>,</#if>${column.sqlName}${lib.space(column.sqlName)} = ${lib.updateVal(column)}
                </#if>
            </#list>
        WHERE
            <#list table.primaryKeys.columnList as column>
                <#if column_index gt 0>AND </#if>${column.sqlName}${lib.space(column.sqlName)} = ${"#"}{${column.javaName},jdbcType=${column.sqlType}}
            </#list>
        ]]>
</#macro>


<#macro autoGen method table>
    <#if method=="insert">
        <@insertMethod table/>
    <#elseif method=="insertBatch">
        <@insertBatchMethod table/>
    <#elseif method=="update">
        <@updateMethod table/>
    </#if>
</#macro>

